package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.vo.BankStationVO;
import com.jishuo.nontax.bfc.service.BaseDataService;
import org.dom4j.Element;
import org.dom4j.Node;

/**
 * @author hlp
 * @date 2023/2/8 15:16
 * @Description:
 */
public class BaseDataApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if (head.equals("findPayway")) { //查询缴款方式，返回全部可用的缴款方式
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            return baseDataService.findPayWay();

//        } else if (head.equals("findPayertype")) { //  按照单位信息获取缴费类型的业务场景，暂时只会在批扣中用到，未来可能延伸到电子票据和网上缴费业务。
//            String unitid = body.selectSingleNode("//root/body/unitid").getText();
//            if(unitid==null||unitid.equals("")){
//                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[cantonid]");
//            }
//            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
//            return baseDataService.findPayerType(Integer.valueOf(unitid));

        } else if (head.equals("findKeyword")) { //获取可用的字轨信息给代理银行的业务场景
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            return baseDataService.findKeyword();

        } else if (head.equals("findCanton")) { //获取可用的行政区划信息给代理银行的业务场景
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            return baseDataService.findCanton();

        } else if (head.equals("findBank")) { //获取可用的银行账户信息给代理银行的业务场景
            String cantonid = this.getNodeText(body.selectSingleNode("//root/body/cantonid"));
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            return baseDataService.findBank(cantonid.length() == 0? null : Long.valueOf(cantonid));

//        }else if (head.equals("confirmBank")) {  //修改代理银行账户信息
//            BankVO bankVO = new BankVO();
//            bankVO.setOperate(Integer.parseInt(body.selectSingleNode("//root/body/operate").getText()));
//            bankVO.setBankid(Long.parseLong(body.selectSingleNode("//root/body/bankid").getText()));
//            bankVO.setBankname(body.selectSingleNode("//root/body/bankname").getText());
//            bankVO.setAccountname(body.selectSingleNode("//root/body/accountname").getText());
//            bankVO.setAccountcode(body.selectSingleNode("//root/body/accountcode").getText());
//            bankVO.setMemo(body.elementText("memo"));
//            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
//            BaseResult ret = baseDataService.confirmBankManage(bankVO);
//            int n = (ret.isSuccess()) ? 1 : 0;
//            ret.setData("<returninfo>"+n+"</returninfo>");
//            return ret;

        }else if(head.equals("saveBankStation")){ //添加银行网点
            String cantonid = body.selectSingleNode("//root/body/cantonid").getText();
            String bankstationcode = body.selectSingleNode("//root/body/bankstationcode").getText();
            String bankstationname = body.selectSingleNode("//root/body/bankstationname").getText();
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            BankStationVO bankStationVO = new BankStationVO();
            bankStationVO.setCantonid(Long.valueOf(cantonid));
            bankStationVO.setBankstationcode(bankstationcode);
            bankStationVO.setBankstationname(bankstationname);
            BaseResult ret = baseDataService.saveBankStation(bankStationVO);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;

        }else if(head.equals("deleteBankStation")){ //根据区域id和网店编码删除银行网点 把状态改成0
            String cantonid = body.selectSingleNode("//root/body/cantonid").getText();
            String bankstationcode = body.selectSingleNode("//root/body/bankstationcode").getText();
            if(cantonid==null||cantonid.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[cantonid]");
            }
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            return baseDataService.deleteBankStation(Integer.parseInt(cantonid), bankstationcode);
        }else if(head.equals("findBankStation")){ //根据区域id 查询银行网点，返回全部可用的银行网点
            String cantonid = body.selectSingleNode("//root/body/cantonid").getText();
            if(cantonid==null||cantonid.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[cantonid]");
            }
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            return baseDataService.findBankStation(Integer.parseInt(cantonid));
        }else if(head.equals("sendBankStation")){ //根据区域id 向财政发送银行网点
            String cantonid = body.selectSingleNode("//root/body/cantonid").getText();
            if(cantonid==null||cantonid.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[cantonid]");
            }
            BaseDataService baseDataService = (BaseDataService) SpringContextUtil.getBean("baseDataService");
            BaseResult ret = baseDataService.sendBankStation(Long.valueOf(cantonid));
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }


        return null;
    }

    /**
     * 获得节点内容
     * @param node 节点
     * @return
     */
    private String getNodeText(Node node) {
        if (node == null) return "";
        return node.getText();
    }
}
